/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.workinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.dao.gen.bean.BizWorkinfo;
import com.nmf.dao.gen.BizWorkinfoMapper;

import com.nmf.site.biz.workinfo.service.BizWorkinfoService;

/**
 * 工作动态Controller
 * @author bm
 * @version 2019-01-27
 */
@Controller
@RequestMapping(value = "${adminPath}/workinfo/bizWorkinfo")
public class BizWorkinfoController extends BaseController {

	@Autowired
	private BizWorkinfoService bizWorkinfoService;
	
	@ModelAttribute
	public BizWorkinfo get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizWorkinfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizWorkinfoService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizWorkinfo();
		}
		return entity;
	}
	
	@RequiresPermissions("workinfo:bizWorkinfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizWorkinfo bizWorkinfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizWorkinfo> page = bizWorkinfoService.findPage(new Page<BizWorkinfo>(request, response), bizWorkinfo); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/workinfo/bizWorkinfoList";
		}
		return "biz/workinfo/bizWorkinfoList";
	}
	
	@RequiresPermissions("workinfo:bizWorkinfo:view")
	@RequestMapping(value = "form")
	public String form(BizWorkinfo bizWorkinfo, Model model) {
		model.addAttribute("bizWorkinfo", bizWorkinfo);
		return "biz/workinfo/bizWorkinfoForm";
	}

	@RequiresPermissions("workinfo:bizWorkinfo:edit")
	@RequestMapping(value = "save")
	public String save(BizWorkinfo bizWorkinfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizWorkinfo)){
			return form(bizWorkinfo, model);
		}
		try{
			bizWorkinfoService.save(bizWorkinfo);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/workinfo/bizWorkinfo/?repage";
		}
		addMessage(redirectAttributes, "保存工作动态成功");
		return "redirect:"+Global.getAdminPath()+"/workinfo/bizWorkinfo/?repage";
	}
	
	@RequiresPermissions("workinfo:bizWorkinfo:edit")
	@RequestMapping(value = "delete")
	public String delete(BizWorkinfo bizWorkinfo, RedirectAttributes redirectAttributes) {
		try{
			bizWorkinfoService.delete(bizWorkinfo);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/workinfo/bizWorkinfo/?repage";
		}
		addMessage(redirectAttributes, "删除工作动态成功");
		return "redirect:"+Global.getAdminPath()+"/workinfo/bizWorkinfo/?repage";
	}

}